var vue=new Vue({
    el:'#main',
    data:{
        name:'',
        oldprivileges:[],
        newprivileges:[]
    },
    methods:{
        addPrivilege:function (privilege) {
            for (let i = 0; i < this.oldprivileges.length; i++) {
                if (this.oldprivileges[i].privilegeId==privilege.privilegeId){
                    this.oldprivileges[i].checked=!this.oldprivileges[i].checked;
                    if (this.oldprivileges[i].checked){
                        this.newprivileges.push(this.oldprivileges[i]);
                    }
                    else {
                        for (let j = 0; j < this.newprivileges.length; j++) {
                            if (this.oldprivileges[i].roleId==this.newprivileges[j].roleId){
                                this.newprivileges.splice(j,1);
                            }
                        }
                    }
                }
            }
            console.log(this.newprivileges);
        },
        doInsert:function () {
            if (name==''){
                layer.msg("名称不能为空");
                return;
            }
            if (this.newprivileges.length>0){
                axios({
                    url:'/role/doInsert',
                    method:'put',
                    data:{
                        name:this.name,
                        privileges:this.newprivileges
                    }
                }).then(response=>{
                    if (response.data.success){
                        parent.layer.message='success';
                        let index=parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    }
                }).catch(error=>{
                    layer.msg(error.method);
                })
            }else {
                layer.msg("至少选一个权限");
                return;
            }
        },
        selectPrivileges:function () {
            axios({
                url:`/privilege/selectAll`,//   有/斜杆的是绝对路径，没有的是相对路径
                method:'get',
            }).then(response=>{
                //layer.msg("查询成功");
                this.oldprivileges=response.data.obj;
                for (let i = 0; i < this.oldprivileges.length; i++) {
                    Vue.set(this.oldprivileges[i],"checked",false);
                }
            }).catch(error=>{
                layer.msg(error.message)
            })
        }
    },
    created:function () {
        this.selectPrivileges();
    }
});